package com.yx.p6_temperaturetest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.yx.YxDeviceManager;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.TimeZone;

public class MainActivity extends AppCompatActivity {
    ScrollView scrollViewl;
    YxDeviceManager yxDeviceManager;
    Button setting;
    TextView time_tem;
    TextView set_tem;
    CheckBox normal_temperature;
    TestResult testResult;
    Spinner spinner;
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch(msg.what){
                case 1:
                    time_tem.setText("实时温度： "+String.valueOf(yxDeviceManager.getTempStatus()));
                    Log.d("TAG", "handleMessage: ");
                    mHandler.sendEmptyMessageDelayed(1, 500);
                    break;
                    case 2:
                        SimpleDateFormat formatter = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss"); //设置时间格式

                        formatter.setTimeZone(TimeZone.getTimeZone("GMT+08")); //设置时区

                        Date curDate = new Date(System.currentTimeMillis()); //获取当前时间
                        String createDate = formatter.format(curDate);

                        Log.d("TAG", "onCreate: "+createDate);
                        testResult.writeData(createDate+" 实时温度："+yxDeviceManager.getTempStatus()+" 度"+ "\r\n");

                        mHandler.sendEmptyMessageDelayed(2, 1000);

                    break;

                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        initView();
        mHandler.sendEmptyMessageDelayed(2, 500);

        mHandler.sendEmptyMessageDelayed(1, 500);
//        scrollViewl.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                Log.d("TAG", "onTouch: "+v.getId());
//                return false;
//            }
//        });
//        scrollViewl.setOnScrollChangeListener(new View.OnScrollChangeListener() {
//            @Override
//            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
//
//            }
//        });
//        listView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
//            @Override
//            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
//                Log.d("TAG", "onScrollChange: "+v.getId());
//            }
//        });

//        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                tem=position;
//                set_tem.setText("设置温度："+String.valueOf(position-40));
//                Log.d("TAG", "onItemClick: "+position);
//            }
//        });
        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getId()==R.id.setting){
                    yxDeviceManager.setTempConf(tem-40);
                }
            }
        });
        normal_temperature.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    Log.d("TAG", "onCheckedChanged: ");
                    yxDeviceManager.setCompressStatus(0);
                } else if (!isChecked) {
                    Log.d("TAG", "onCheckedChanged1: ");
                    yxDeviceManager.setCompressStatus(2);
                }
            }
        });
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                tem=position;
                set_tem.setText("设置温度："+String.valueOf(position-40));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }
    ListView listView;
    int tem=0;
    private void initView() {
        testResult= new TestResult();
        spinner =findViewById(R.id.selectcode);
        normal_temperature = findViewById(R.id.normal_temperature);
        time_tem = findViewById(R.id.time_tem);
        set_tem = findViewById(R.id.set_tem);
        setting = findViewById(R.id.setting);
        yxDeviceManager = YxDeviceManager.getInstance(this);
//        listView = (ListView)findViewById(R.id.lv);
//        scrollViewl = findViewById(R.id.scrollView2);
        ArrayList list  = new ArrayList();
        for(int i=-40;i<81;i++){

            list.add(i+"摄氏度");
        }
        ArrayAdapter adapter = new ArrayAdapter(this,R.layout.list,list);
        spinner.setAdapter(adapter);
//        listView.setAdapter(adapter);
    }

    @Override
    protected void onDestroy() {
        mHandler.removeCallbacksAndMessages(null);
        super.onDestroy();
    }
}